import { Provide } from '@midwayjs/decorator';
import { IWebMiddleware, IMidwayWebNext } from '@midwayjs/web';
import { Context } from 'egg';

import * as formate from 'date-format';

@Provide()
export class ReportMiddleware implements IWebMiddleware {
  resolve() {
    return async (ctx: Context, next: IMidwayWebNext) => {
      const { method, url } = ctx.request;
      const time = formate('yyyy-MM-dd hh:mm:ss', new Date());
      const methondColor = method == 'POST' ? '32' : '34';
      console.log(
        `\x1B[33m[ ${time} ]\x1B[0m  \x1B[${methondColor}m${method}\x1B[0m ${url}`
      );
      await next();
      // console.log(ctx.body);
    };
  }
}
